const mongoose = require("mongoose");
const debug = require("debug")("back-end:server");

const connect = async () => {
  mongoose.connection.on("error", (error) => {
    console.error("* MongoDB : connection error:", error);
    process.exit(1);
  });

  mongoose.connection.once("open", () => {
    debug("* MongoDB : connected");
  });

  try {
    await mongoose.connect(process.env["MONGODB_ATLAS_URI"]);
  } catch (error) {
    console.error("* MongoDB : connection error:", error);
    process.exit(1);
  }
};

module.exports = {
  connect,
};
